package com.example.hosptial.newController;

import com.example.hosptial.model.enity.Admin;
import com.example.hosptial.service.AuthService;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 管理员认证控制器
 */
@Controller
@RequestMapping("/admin/auth")
@Validated
public class AdminAuthController {

    private static final Logger logger = LoggerFactory.getLogger(AdminAuthController.class);

    @Autowired
    private AuthService authService;

    /**
     * 显示登录页面
     */
    @GetMapping("/login")
    public String showLoginPage() {
        return "common/login";
    }

    /**
     * 管理员登录
     */
    @PostMapping("/login")
    public String login(
            @NotBlank(message = "用户名不能为空") @RequestParam String username,
            @NotBlank(message = "密码不能为空") @RequestParam String password,
            HttpSession session,
            RedirectAttributes redirectAttributes) {

        try {
            Admin admin = authService.adminLogin(username, password);

            if (admin != null) {
                // 创建会话
                authService.createAdminSession(session, admin);
                return "redirect:/admin/index";  // 重定向到管理员主页
            } else {
                redirectAttributes.addFlashAttribute("error", "用户名或密码错误");
                return "redirect:/admin/auth/login";
            }

        } catch (Exception e) {
            logger.error("登录失败: {}", e.getMessage());
            redirectAttributes.addFlashAttribute("error", "登录失败: " + e.getMessage());
            return "redirect:/admin/auth/login";
        }
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public String logout(HttpSession session, RedirectAttributes redirectAttributes) {
        try {
            if (authService.validateAdminSession(session)) {
                authService.logout(session);
                redirectAttributes.addFlashAttribute("message", "已成功退出登录");
            }
            return "redirect:/admin/auth/login";  // 重定向到登录页面
        } catch (Exception e) {
            logger.error("退出登录失败: {}", e.getMessage());
            redirectAttributes.addFlashAttribute("error", "退出登录失败: " + e.getMessage());
            return "redirect:/admin/auth/login";
        }
    }

    /**
     * 检查登录状态
     */
    @GetMapping("/check")
    public String checkLoginStatus(HttpSession session, Model model) {
        boolean isLoggedIn = authService.validateAdminSession(session);
        if (isLoggedIn) {
            Admin admin = (Admin) session.getAttribute("admin");
            model.addAttribute("admin", admin);
            return "admin/index";  // 显示管理员主页
        } else {
            return "redirect:/admin/auth/login";  // 重定向到登录页面
        }
    }
}